<template>
  <div class="delivery-print">
    <!-- 打印头部 -->
    <div class="print-header">
      <div class="header-content">
        <div class="header-left">
          <div class="header-icon">
            <van-icon name="printer-o" size="24" />
          </div>
          <div class="header-text">
            <h2>送货单打印预览</h2>
            <p>预览送货单格式，确认无误后执行打印</p>
          </div>
        </div>
        <div class="print-actions">
          <van-button 
            type="primary" 
            size="large"
            icon="printer-o"
            @click="printDelivery"
            class="action-btn print"
          >
            立即打印
          </van-button>
          <van-button 
            size="large"
            icon="arrow-left"
            @click="goBack"
            class="action-btn back"
          >
            返回
          </van-button>
        </div>
      </div>
    </div>

    <!-- 打印内容 -->
    <div class="print-content" ref="printContent">
      <!-- 送货单头部 -->
      <div class="delivery-header">
        <div class="company-info">
          <div class="company-logo">
            <van-icon name="shop-o" size="32" />
          </div>
          <h1 class="company-name">ERP系统送货单</h1>
          <div class="company-details">
            <div class="detail-row">
              <van-icon name="location-o" size="14" />
              <span>公司地址：公司地址</span>
            </div>
            <div class="detail-row">
              <van-icon name="phone-o" size="14" />
              <span>联系电话：联系电话</span>
            </div>
            <div class="detail-row">
              <van-icon name="description" size="14" />
              <span>传真号码：传真号码</span>
            </div>
          </div>
        </div>
        <div class="delivery-info">
          <div class="info-card">
            <div class="info-row">
              <span class="label">送货单号：</span>
              <span class="value">{{ delivery.shipment_no }}</span>
            </div>
            <div class="info-row">
              <span class="label">送货日期：</span>
              <span class="value">{{ delivery.delivery_date }}</span>
            </div>
            <div class="info-row">
              <span class="label">打印时间：</span>
              <span class="value">{{ printTime }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 客户信息 -->
      <div class="customer-section">
        <div class="section-header">
          <van-icon name="contact" size="18" />
          <h3>客户信息</h3>
        </div>
        <div class="customer-grid">
          <div class="info-item">
            <span class="label">客户名称：</span>
            <span class="value">{{ delivery.customer }}</span>
          </div>
          <div class="info-item">
            <span class="label">联系电话：</span>
            <span class="value">{{ delivery.customer_phone }}</span>
          </div>
          <div class="info-item">
            <span class="label">送货地址：</span>
            <span class="value">{{ delivery.delivery_address }}</span>
          </div>
          <div class="info-item">
            <span class="label">联系人：</span>
            <span class="value">{{ delivery.delivery_contact }}</span>
          </div>
        </div>
      </div>

      <!-- 商品清单 -->
      <div class="items-section">
        <div class="section-header">
          <van-icon name="orders-o" size="18" />
          <h3>商品清单</h3>
        </div>
        <div class="table-container">
          <table class="items-table">
            <thead>
              <tr>
                <th>序号</th>
                <th>商品名称</th>
                <th>规格</th>
                <th>单位</th>
                <th>数量</th>
                <th>单价</th>
                <th>小计</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, index) in delivery.items" :key="item.id">
                <td>{{ index + 1 }}</td>
                <td>{{ item.product_name }}</td>
                <td>{{ item.spec || '-' }}</td>
                <td>{{ item.unit || '个' }}</td>
                <td>{{ item.quantity }}</td>
                <td>¥{{ item.price?.toFixed(2) || '0.00' }}</td>
                <td>¥{{ (item.quantity * (item.price || 0)).toFixed(2) }}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>

      <!-- 费用汇总 -->
      <div class="summary-section">
        <div class="section-header">
          <van-icon name="balance-o" size="18" />
          <h3>费用汇总</h3>
        </div>
        <div class="summary-grid">
          <div class="summary-item">
            <span class="label">商品总价：</span>
            <span class="value">¥{{ delivery.total_price?.toFixed(2) || '0.00' }}</span>
          </div>
          <div class="summary-item">
            <span class="label">折扣：</span>
            <span class="value">¥{{ delivery.discount?.toFixed(2) || '0.00' }}</span>
          </div>
          <div class="summary-item">
            <span class="label">税率：</span>
            <span class="value">{{ delivery.tax_rate || 0 }}%</span>
          </div>
          <div class="summary-item">
            <span class="label">税额：</span>
            <span class="value">¥{{ delivery.tax_amount?.toFixed(2) || '0.00' }}</span>
          </div>
          <div class="summary-item">
            <span class="label">运费：</span>
            <span class="value">¥{{ delivery.freight?.toFixed(2) || '0.00' }}</span>
          </div>
          <div class="summary-item total">
            <span class="label">最终金额：</span>
            <span class="value">¥{{ delivery.final_amount?.toFixed(2) || '0.00' }}</span>
          </div>
        </div>
      </div>

      <!-- 出库发货信息 -->
      <div class="shipping-section">
        <div class="section-header">
          <van-icon name="logistics" size="18" />
          <h3>出库发货信息</h3>
        </div>
        <div class="shipping-grid">
          <div class="info-item">
            <span class="label">出库仓库：</span>
            <span class="value">{{ delivery.warehouse }}</span>
          </div>
          <div class="info-item">
            <span class="label">包装员：</span>
            <span class="value">{{ delivery.packager || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">发货员：</span>
            <span class="value">{{ delivery.shipper || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">运输方式：</span>
            <span class="value">{{ delivery.shipping_method || '-' }}</span>
          </div>
        </div>
      </div>

      <!-- 备注信息 -->
      <div class="notes-section" v-if="delivery.notes">
        <div class="section-header">
          <van-icon name="comment-o" size="18" />
          <h3>备注</h3>
        </div>
        <div class="notes-content">
          <p>{{ delivery.notes }}</p>
        </div>
      </div>

      <!-- 签名区域 -->
      <div class="signature-section">
        <div class="section-header">
          <van-icon name="edit" size="18" />
          <h3>签名确认</h3>
        </div>
        <div class="signature-grid">
          <div class="signature-item">
            <span class="label">客户签名：</span>
            <div class="signature-line">_________________</div>
          </div>
          <div class="signature-item">
            <span class="label">发货员签名：</span>
            <div class="signature-line">_________________</div>
          </div>
          <div class="signature-item">
            <span class="label">日期：</span>
            <div class="signature-line">_________________</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast, showSuccessToast, showFailToast } from 'vant'
import { deliveryApi } from '@/api/delivery'

const route = useRoute()
const router = useRouter()
const printContent = ref<HTMLElement>()

const delivery = ref<any>({})
const printTime = computed(() => {
  return new Date().toLocaleString('zh-CN')
})

onMounted(async () => {
  const orderId = route.params.orderId
  if (orderId) {
    await loadDeliveryData(Number(orderId))
  }
})

const loadDeliveryData = async (orderId: number) => {
  try {
    const response = await deliveryApi.print(orderId)
    delivery.value = response.data.order
  } catch (error) {
    showFailToast('加载送货单数据失败')
    console.error('加载送货单数据失败:', error)
  }
}

const printDelivery = () => {
  if (!printContent.value) return
  
  const printWindow = window.open('', '_blank')
  if (printWindow) {
    printWindow.document.write(`
      <html>
        <head>
          <title>送货单 - ${delivery.value.shipment_no}</title>
          <style>
            body { 
              font-family: 'Microsoft YaHei', Arial, sans-serif; 
              margin: 20px; 
              font-size: 14px; 
              line-height: 1.6;
            }
            .delivery-header { 
              text-align: center; 
              margin-bottom: 30px; 
              border-bottom: 2px solid #333; 
              padding-bottom: 20px;
            }
            .company-name { 
              font-size: 24px; 
              font-weight: bold; 
              margin: 0 0 10px 0; 
            }
            .company-details { 
              font-size: 12px; 
              color: #666; 
            }
            .customer-section, .items-section, .summary-section, .shipping-section, .notes-section, .signature-section { 
              margin-bottom: 25px; 
            }
            h3 { 
              border-bottom: 1px solid #ccc; 
              padding-bottom: 5px; 
              margin-bottom: 15px; 
            }
            .customer-grid, .summary-grid, .shipping-grid, .signature-grid { 
              display: grid; 
              grid-template-columns: 1fr 1fr; 
              gap: 15px; 
            }
            .info-item, .summary-item, .signature-item { 
              display: flex; 
              justify-content: space-between; 
            }
            .label { 
              font-weight: bold; 
              color: #333; 
            }
            .value { 
              color: #666; 
            }
            .total { 
              font-weight: bold; 
              font-size: 16px; 
              border-top: 1px solid #ccc; 
              padding-top: 10px; 
            }
            .items-table { 
              width: 100%; 
              border-collapse: collapse; 
              margin-bottom: 20px; 
            }
            .items-table th, .items-table td { 
              border: 1px solid #ccc; 
              padding: 8px; 
              text-align: center; 
            }
            .items-table th { 
              background-color: #f5f5f5; 
              font-weight: bold; 
            }
            .signature-line { 
              border-bottom: 1px solid #333; 
              min-width: 150px; 
              text-align: center; 
            }
            @media print {
              body { margin: 0; }
              .print-actions { display: none; }
            }
          </style>
        </head>
        <body>
          ${printContent.value.innerHTML}
        </body>
      </html>
    `)
    printWindow.document.close()
    printWindow.focus()
    printWindow.print()
    printWindow.close()
    showSuccessToast('打印指令已发送')
  } else {
    showFailToast('无法打开打印窗口')
  }
}

const goBack = () => {
  router.go(-1)
}
</script>

<style scoped>
.delivery-print {
  padding: 16px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
}

.print-header {
  background: linear-gradient(to right, #667eea, #764ba2);
  border-radius: 16px;
  padding: 24px;
  margin-bottom: 24px;
  box-shadow: 0 8px 25px rgba(0,0,0,0.15);
  color: white;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-icon {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
}

.header-text h2 {
  margin: 0;
  font-size: 28px;
  font-weight: bold;
}

.header-text p {
  margin: 8px 0 0 0;
  font-size: 14px;
  opacity: 0.9;
}

.print-actions {
  display: flex;
  gap: 12px;
}

.action-btn {
  border-radius: 10px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  font-size: 16px;
}

.action-btn.print {
  background: linear-gradient(45deg, #ff6b6b, #ee5a24);
  border: none;
  box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
}

.action-btn.back {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(10px);
}

.print-content {
  background: white;
  padding: 40px;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  max-width: 900px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

.print-content::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(to right, #667eea, #764ba2);
}

.delivery-header {
  text-align: center;
  margin-bottom: 40px;
  padding-bottom: 30px;
  border-bottom: 2px solid #f0f0f0;
  position: relative;
}

.company-logo {
  background: linear-gradient(45deg, #667eea, #764ba2);
  border-radius: 50%;
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
  color: white;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.company-name {
  font-size: 32px;
  font-weight: bold;
  margin: 0 0 20px 0;
  color: #333;
  background: linear-gradient(45deg, #667eea, #764ba2);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.company-details {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}

.detail-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #666;
}

.detail-row .van-icon {
  color: #667eea;
}

.delivery-info {
  position: absolute;
  top: 0;
  right: 0;
  background: linear-gradient(45deg, #f8f9fa, #e9ecef);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.info-card {
  text-align: right;
}

.info-row {
  display: flex;
  justify-content: space-between;
  margin: 8px 0;
  font-size: 14px;
}

.info-row .label {
  font-weight: bold;
  color: #333;
  margin-right: 12px;
}

.info-row .value {
  color: #667eea;
  font-weight: 500;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #f0f0f0;
}

.section-header h3 {
  margin: 0;
  color: #333;
  font-size: 20px;
  font-weight: bold;
}

.section-header .van-icon {
  color: #667eea;
  background: rgba(102, 126, 234, 0.1);
  border-radius: 8px;
  padding: 8px;
}

.customer-section, .items-section, .summary-section, .shipping-section, .notes-section, .signature-section {
  margin-bottom: 35px;
}

.customer-grid, .summary-grid, .shipping-grid, .signature-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.info-item, .summary-item, .signature-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 10px;
  border-left: 4px solid #667eea;
  transition: all 0.3s ease;
}

.info-item:hover, .summary-item:hover, .signature-item:hover {
  background: #e9ecef;
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.label {
  font-weight: bold;
  color: #333;
  font-size: 14px;
}

.value {
  color: #666;
  font-size: 14px;
  font-weight: 500;
}

.total {
  font-weight: bold;
  font-size: 18px;
  border-top: 2px solid #667eea;
  padding-top: 20px;
  grid-column: 1 / -1;
  background: linear-gradient(45deg, #667eea, #764ba2) !important;
  color: white;
}

.total .label, .total .value {
  color: white;
}

.table-container {
  background: #f8f9fa;
  border-radius: 12px;
  padding: 20px;
  overflow-x: auto;
}

.items-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 0;
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.items-table th, .items-table td {
  border: 1px solid #e9ecef;
  padding: 12px 8px;
  text-align: center;
  font-size: 14px;
}

.items-table th {
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
  font-weight: bold;
  font-size: 14px;
}

.items-table tr:nth-child(even) {
  background: #f8f9fa;
}

.items-table tr:hover {
  background: #e9ecef;
}

.notes-content {
  background: #f8f9fa;
  padding: 20px;
  border-radius: 12px;
  border-left: 4px solid #667eea;
}

.notes-content p {
  margin: 0;
  color: #666;
  font-size: 14px;
  line-height: 1.6;
}

.signature-line {
  border-bottom: 2px solid #333;
  min-width: 180px;
  text-align: center;
  padding: 8px 0;
  font-size: 16px;
  color: #333;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .print-content {
    padding: 20px;
    margin: 0 10px;
  }
  
  .header-content {
    flex-direction: column;
    gap: 20px;
    text-align: center;
  }
  
  .print-actions {
    width: 100%;
    justify-content: center;
  }
  
  .action-btn {
    flex: 1;
    max-width: 200px;
  }
  
  .company-logo {
    width: 60px;
    height: 60px;
  }
  
  .company-name {
    font-size: 24px;
  }
  
  .delivery-info {
    position: static;
    margin-top: 20px;
  }
  
  .info-card {
    text-align: center;
  }
  
  .customer-grid, .summary-grid, .shipping-grid, .signature-grid {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .info-item, .summary-item, .signature-item {
    padding: 12px;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .section-header h3 {
    font-size: 18px;
  }
  
  .table-container {
    padding: 10px;
  }
  
  .items-table th, .items-table td {
    padding: 8px 4px;
    font-size: 12px;
  }
}

/* 打印样式 */
@media print {
  .print-header {
    display: none;
  }
  
  .print-content {
    box-shadow: none;
    padding: 0;
    margin: 0;
    border-radius: 0;
  }
  
  .print-content::before {
    display: none;
  }
  
  .company-logo {
    background: #333 !important;
    -webkit-print-color-adjust: exact;
    color-adjust: exact;
  }
  
  .company-name {
    background: #333 !important;
    -webkit-background-clip: unset;
    -webkit-text-fill-color: unset;
    color: #333 !important;
  }
  
  .total {
    background: #333 !important;
    -webkit-print-color-adjust: exact;
    color-adjust: exact;
  }
  
  .items-table th {
    background: #333 !important;
    -webkit-print-color-adjust: exact;
    color-adjust: exact;
  }
  
  body {
    background: white;
  }
  
  .info-item:hover, .summary-item:hover, .signature-item:hover {
    transform: none;
    box-shadow: none;
  }
}
</style> 